package com.xsq.netty.utils;

import com.tencent.mars.xlog.Log;

/**
 * Created by Shiquan.Xiao on 2023/10/14.
 */
public class LogUtils {

    private static boolean isOpenLog = false;

    public static void init(
            String namePrefix,
            String logPath,
            String cachePath,
            boolean debug) {
        isOpenLog = debug;
        System.loadLibrary("c++_shared");
        System.loadLibrary("marsxlog");
        Log.setLogImp(new com.tencent.mars.xlog.Xlog());
        if (debug) {
            Log.setConsoleLogOpen(true);
            Log.appenderOpen(
                    com.tencent.mars.xlog.Xlog.LEVEL_VERBOSE,
                    com.tencent.mars.xlog.Xlog.AppednerModeAsync,
                    cachePath,
                    logPath,
                    namePrefix,
                    3
            );
        } else {
            Log.setConsoleLogOpen(false);
            Log.appenderOpen(
                    com.tencent.mars.xlog.Xlog.LEVEL_INFO,
                    com.tencent.mars.xlog.Xlog.AppednerModeAsync,
                    cachePath,
                    logPath,
                    namePrefix,
                    3
            );
        }
    }

    public static void  d(String tag, String msg) {
        if (!isOpenLog) {
            android.util.Log.d(tag, msg);
        } else {
            Log.d(tag, msg);
        }
    }

    public static void  v(String tag, String msg) {
        if (!isOpenLog) {
            android.util.Log.v(tag, msg);
        } else {
            Log.v(tag, msg);
        }
    }

    public static void  i(String tag, String msg) {
        if (!isOpenLog) {
            android.util.Log.i(tag, msg);
        } else {
            Log.i(tag, msg);
        }
    }

    public static void  w(String tag, String msg) {
        if (!isOpenLog) {
            android.util.Log.w(tag, msg);
        } else {
            Log.w(tag, msg);
        }
    }

    public static void  w(String tag, String msg, Throwable t) {
        if (!isOpenLog) {
            android.util.Log.w(tag, "msg: "+msg+ "throwable:"+android.util.Log.getStackTraceString(t));
        } else {
            Log.w(tag, "msg: %s, throwable: %s", msg, android.util.Log.getStackTraceString(t));
        }
    }

    public static void  e(String tag, String msg) {
        if (!isOpenLog) {
            android.util.Log.e(tag, msg);
        } else {
            Log.e(tag, msg);
        }
    }

    public static void  e(String tag, String msg, Throwable t) {
        if (!isOpenLog) {
            android.util.Log.e(tag, msg);
        } else {
            Log.e(tag, "msg: %s, throwable: ", msg, t);
            Log.printErrStackTrace(tag, t, "msg: %s", msg);
        }
    }
}
